package com.cweb.admin.serivce.wxMp;

import cn.hutool.core.util.StrUtil;
import com.cweb.framework.pojo.CustomException;
import com.cweb.framework.util.ErrorEnum;
import com.cweb.admin.controller.param.WxMpVerifyParam;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WeixinPortalService {
    @Autowired
    private WeixinMpSerivce weixinMpSerivce;

    /**
     * 微信接受消息校验并取得服务对象
     */
    public WxMpService wxParamVerify(Long platformId, WxMpVerifyParam param) throws CustomException {
        // 非空校验
        if(StrUtil.hasBlank(param.getNonce(), param.getSignature(), param.getTimestamp())){
            throw new CustomException(ErrorEnum.HTTP_PARAM_NULL);
        }
        // 微信工具，参数校验
        WxMpService wxService = weixinMpSerivce.init(platformId);
        if(!wxService.checkSignature(param.getTimestamp(), param.getNonce(), param.getSignature())){
            throw new CustomException(ErrorEnum.API_WEIXIN_API_VERIFY_ERROR);
        }
        return wxService;
    }
}
